package demo1;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

/**
 * 2024/6/7 案例大纲
 */
public class Test1 {
  public static  List<People> people= new ArrayList<>();

    public static void main(String[] args) {
       //1.创建100个囚犯,依次占位,并为其赋值编号(1-200之间的随机数,不能重复)

        Random r=new Random();

        //2.循环100次,创建100个囚犯,存入集合
        for (int i = 1; i <=100 ; i++) {
            while (true) {
                int code = r.nextInt(200) + 1;
                if (exist(code)){
                    People p=new People(code, i);
                    people.add(p);
                    break;
                }
            }

        }

        System.out.println("囚犯占位"+people);



        //4.反复删除奇数位置处的人, 直到剩余一个人为止。
        while (people.size()>1){
            List<People> arrayList = new ArrayList<>();
            for (int i = 1; i < people.size(); i+=2) {
                People p = people.get(i);
                arrayList.add(p);
            }
            people=arrayList;
        }

        People luckPeople = people.get(0);
        System.out.println(luckPeople);
    }


    public static boolean exist(int code){
        for (People peoples : people) {
          if(peoples.getCode()==code){
              return false;
          }
        }
        return true;
    }
}
